<?php
namespace WebApper\Transaction;

class Transaction extends \WebApper\Base {

	/**
	 * The user_payment_master_id
	 *
	 * @var integer
	 */
	var $user_payment_master_id;

	/**
	 * The user_id
	 *
	 * @var integer
	 */
	var $user_id;

	/**
	 * The client_name
	 *
	 * @var string
	 */
	var $client_name;

	/**
	 * The co_client name
	 *
	 * @var string
	 */
	var $co_client;

	/**
	 * The contracted amount
	 *
	 * @var integer
	 */
	var $contracted_amount;

	/**
	 * The interest rate
	 *
	 * @var decimal
	 */
	var $interest_rate;

	/**
	 * The start date
	 *
	 * @var date
	 */
	var $start_date;

	/**
	 *  The end date
	 *
	 * @var date
	 */
	var $end_date;

	/**
	 * The sales representative signing up 
	 *
	 * @var string
	 */
	var $sales_rep_signed_up;

	/**
	 * The consult reporting assign
	 *
	 * @var string
	 */
	var $consult_rep_assigned_to;

	/**
	 * The card holder name
	 *
	 * @var string
	 */
	var $card_holder_name;
	
	/**
	 * The client autoconfirm email
	 *
	 * @var string
	 */
	var $card_type;
	
	/**
	 * The card number
	 *
	 * @var string
	 */
	var $card_number;
	
	/**
	 * The card expiration month
	 *
	 * @var date
	 */
	var $card_expiration_month;
	
	/**
	 * The card expiration year
	 *
	 * @var date
	 */
	var $card_expiration_year;
	
	/**
	 * The transaction status
	 *
	 * @var string
	 */
	var $status;
	
	/**
	 * The card holder country name
	 *
	 * @var string
	 */
	var $card_country;
	/**
	 * The card holder address
	 *
	 * @var string
	 */
	var $card_address;
	/**
	 * The card holder city
	 *
	 * @var string
	 */
	var $card_city;
	/**
	 * The card holder state
	 *
	 * @var string
	 */
	var $card_state;
	/**
	 * The card security code
	 *
	 * @var string
	 */
	var $card_ccv;
	/**
	 * The card holder zip code
	 *
	 * @var integer
	 */
	var $card_zip_code;
	/**
	 * The bank routing number
	 *
	 * @var string
	 */
	var $bank_routing_number;
	/**
	 * The bank account number
	 *
	 * @var string
	 */
	var $bank_account_number;
	/**
	 * The bank check number
	 *
	 * @var string
	 */
	var $bank_check_number;
	/**
	 * The principal amount 	
	 *
	 * @var integer
	 */
	var $principal_amount;
	/**
	 * The trail amount 	
	 *
	 * @var integer
	 */
	var $trail_amount;
	/**
	 * The  trail length 	
	 *
	 * @var string
	 */
	var $trail_length;
	/**
	 * The  term length 	
	 *
	 * @var string
	 */
	var $term_length;  
	/**
	 * The  billing frequency	
	 *
	 * @var string
	 */
	var $billing_frequency; 
	/**
	 * The  billing date	
	 *
	 * @var date
	 */
	var $billing_date;  
			
/** Public Methods ********************************************************/

	/**
	 * Constructor
	 *
	 * @param integer $user_id
	 */
	function __construct(  $user_id = 0 ) {
		if( !empty( $user_id ) ){
			$this->user_id = $user_id;
			$this->populate();
		}
	}

	/**
	 * Update or insert user payment master details into the database.
	 * @global wpdb $wpdb WordPress database object
	 * @return bool Success or failure
	 */
	function save() {
		global $webapper, $wpdb;
		// Update
		if ( !empty( $this->user_payment_master_id ) ) {
							
			$result = $wpdb->update( 
			$webapper->tables->user_payment_master, 
			array( 
				 'user_id'        			=> $this->user_id,
				 'client_name' 				=> $this->client_name,
				 'co_client' 				=> $this->co_client,
				 'contracted_amount' 		=> $this->contracted_amount,
				 'interest_rate' 			=> $this->interest_rate,
				 'start_date' 				=> $this->start_date,
				 'end_date' 				=> $this->end_date,
				 'sales_rep_signed_up'  	=> $this->sales_rep_signed_up,
				 'consult_rep_assigned_to' 	=> $this->consult_rep_assigned_to,
				 'card_holder_name' 		=> $this->card_holder_name,
				 'card_type'				=> $this->card_type,
				 'card_number' 				=> $this->card_number,
				 'card_expiration_month' 	=> $this->card_expiration_month,
				 'card_expiration_year' 	=> $this->card_expiration_year,
				 'status' 					=> $this->status,
				 'card_country' 			=> $this->card_country,
				 'card_address' 			=> $this->card_address,
				 'card_city' 				=> $this->card_city,
				 'card_state' 				=> $this->card_state,
				 'card_ccv' 				=> $this->card_ccv,
				 'card_zip_code' 			=> $this->card_zip_code,
				 'bank_routing_number' 		=> $this->bank_routing_number,
				 'bank_account_number' 		=> $this->bank_account_number,
				 'bank_check_number' 		=> $this->bank_check_number,	
				 'principal_amount'         => $this->principal_amount,
				 'trail_amount'             => $this->trail_amount,
				 'trail_length'             => $this->trail_length,
				 'term_length'              => $this->term_length,
				 'billing_frequency'        => $this->billing_frequency,
				 'billing_date'             => $this->billing_date
			), 
			array( 'user_id' => $this->user_id ), 
			array( '%d', '%s', '%s', '%f', '%f', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'), 
			array( '%d' ) 
		);	
		    //insert
			} else {
				$result = $wpdb->insert( 
		$webapper->tables->user_payment_master, 
		array( 
			 'user_id'        			=> $this->user_id,
			 'client_name' 				=> $this->client_name,
			 'co_client' 				=> $this->co_client,
			 'contracted_amount' 		=> $this->contracted_amount,
			 'interest_rate' 			=> $this->interest_rate,
			 'start_date' 				=> $this->start_date,
			 'end_date' 				=> $this->end_date,
			 'sales_rep_signed_up'  	=> $this->sales_rep_signed_up,
			 'consult_rep_assigned_to' 	=> $this->consult_rep_assigned_to,
			 'card_holder_name' 		=> $this->card_holder_name,
			 'card_type'				=> $this->card_type,
			 'card_number' 				=> $this->card_number,
			 'card_expiration_month' 	=> $this->card_expiration_month,
			 'card_expiration_year' 	=> $this->card_expiration_year,
			 'status' 					=> $this->status,
			 'card_country' 			=> $this->card_country,
			 'card_address' 			=> $this->card_address,
			 'card_city' 				=> $this->card_city,
			 'card_state' 				=> $this->card_state,
			 'card_ccv' 				=> $this->card_ccv,
			 'card_zip_code' 			=> $this->card_zip_code,
			 'bank_routing_number' 		=> $this->bank_routing_number,
			 'bank_account_number' 		=> $this->bank_account_number,
			 'bank_check_number' 		=> $this->bank_check_number,
			 'principal_amount'         => $this->principal_amount,
			 'trail_amount'             => $this->trail_amount,
			 'trail_length'             => $this->trail_length,
			 'term_length'              => $this->term_length,
			 'billing_frequency'        => $this->billing_frequency,
			 'billing_date'             => $this->billing_date	
		), 
		array( '%d', '%s', '%s', '%f', '%f', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')
	);
	$this->user_payment_master_id = $wpdb->insert_id;
	
			}	
		if ( !$result )
			return false;
		return true;
	}

	/** Private Methods *******************************************************/

	/**
	 * Fetches the user payment master data from database.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 */
	private function populate() {
		global $webapper, $wpdb;

		if ( $payment_master = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$webapper->tables->user_payment_master} WHERE user_id = %d", $this->user_id ) ) ) {
			$this->user_payment_master_id	= $payment_master->user_payment_master_id;
			$this->user_id			        = $payment_master->user_id;
			$this->client_name				= $payment_master->client_name;
			$this->co_client	            = $payment_master->co_client;
			$this->contracted_amount		= $payment_master->contracted_amount;
			$this->interest_rate		    = $payment_master->interest_rate;
			$this->start_date			    = $payment_master->start_date;
			$this->end_date		            = $payment_master->end_date;
			$this->sales_rep_signed_up	    = $payment_master->sales_rep_signed_up;
			$this->consult_rep_assigned_to	= $payment_master->consult_rep_assigned_to;
			$this->card_holder_name		    = $payment_master->card_holder_name;
			$this->card_type		        = $payment_master->card_type;
			$this->card_number			    = base64_decode($payment_master->card_number);
			$this->card_expiration_month    = $payment_master->card_expiration_month;
			$this->card_expiration_year	    = $payment_master->card_expiration_year;
			$this->status		            = $payment_master->status;
			$this->card_country		        = $payment_master->card_country;
			$this->card_address             = $payment_master->card_address;
			$this->card_city	            = $payment_master->card_city;
			$this->card_state	            = $payment_master->card_state;
			$this->card_ccv		            = base64_decode($payment_master->card_ccv);
			$this->card_zip_code		    = $payment_master->card_zip_code;
			$this->bank_routing_number	    = base64_decode($payment_master->bank_routing_number);
			$this->bank_account_number		= base64_decode($payment_master->bank_account_number);
			$this->bank_check_number	    = base64_decode($payment_master->bank_check_number);
			$this->principal_amount         = $payment_master->principal_amount;
			$this->trail_amount	            = $payment_master->trail_amount;
			$this->trail_length		        = $payment_master->trail_length;
			$this->term_length	            = $payment_master->term_length;
			$this->billing_frequency	    = $payment_master->billing_frequency;
			$this->billing_date	            = $payment_master->billing_date;
		}	

		if ( $payment_transaction = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$webapper->tables->user_payment_transaction} WHERE user_id = %d", $this->user_id ) ) ) {
			$this->user_payment_transaction_id	= $payment_transaction->user_payment_transaction_id;
			$this->user_id			        	= $payment_transaction->user_id;
			$this->payment_for			    	= $payment_transaction->payment_for;
			$this->received_as			    	= $payment_transaction->received_as;
			$this->amount_paid			    	= $payment_transaction->amount_paid;
			$this->transaction_date				= $payment_transaction->transaction_date;
			$this->transaction_status			= $payment_transaction->transaction_status;
			$this->created_on			    	= $payment_transaction->created_on;
			$this->edited_on			    	= $payment_transaction->edited_on;
		}
	}
}
?>